package com.tjsinfo.tjsoa.system.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.articles.bo.ManageSuperAction;
import com.tjsinfo.tjsoa.system.bo.IChangYongYuBo;
import com.tjsinfo.tjsoa.system.vo.TSystemChangyongyu;
import com.tjsinfo.tjsoa.system.vo.TSystemChangyongyuDefault;
import com.tjsinfo.tjsoa.system.vo.TSystemChangyongyuEntry;
import com.tjsinfo.tjsoa.system.vo.User;

public class SystemChangYongYuAction extends ManageSuperAction{

	private IChangYongYuBo iChangYongYuBo;
	public IChangYongYuBo getiChangYongYuBo() {
		return iChangYongYuBo;
	}
	public void setiChangYongYuBo(IChangYongYuBo iChangYongYuBo) {
		this.iChangYongYuBo = iChangYongYuBo;
	}
    //查询出所有环节
	public ActionForward queryAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<TSystemChangyongyu> list = iChangYongYuBo.queryAll();
		JSONArray array = new JSONArray();
		for(int i=0;i<list.size();i++){
			TSystemChangyongyu systemChangyongyu = (TSystemChangyongyu)list.get(i);
			JSONObject jsonObject = new JSONObject(systemChangyongyu);
			array.put(jsonObject);
		}
		JSONObject object = new JSONObject();
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}
	//查出默认用语
	public ActionForward queryAllChangYongYuDefalt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("mainId");
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		if(start == null){
			start = "0";
		}
		if(limit == null){
			limit = "10";
		}
		List<TSystemChangyongyuDefault> list = iChangYongYuBo.queryChangyongyuDefaults(id,start,limit);
		int size = iChangYongYuBo.queryTotalDefalt(id);
		JSONArray array = new JSONArray();
		for(int i=0;i<list.size();i++){
			TSystemChangyongyuDefault systemChangyongyuDefault = (TSystemChangyongyuDefault)list.get(i);
			JSONObject jsonObject = new JSONObject(systemChangyongyuDefault);
			array.put(jsonObject);
		}
		JSONObject object= new JSONObject();
		object.put("totalProperty", Integer.toString(size));
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}
	//查出个人用语
	public ActionForward queryAllChangYongYuPersonal(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("mainId");
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		if(start == null) start="0";
		if(limit == null) limit="10";
		List<TSystemChangyongyuEntry> list = iChangYongYuBo.queryChangyongyuEntries(id, start, limit);
		int size = iChangYongYuBo.queryTotalPerson(id);
		JSONArray array = new JSONArray();
		for(int i=0;i<list.size();i++){
			TSystemChangyongyuEntry systemChangyongyuEntry = (TSystemChangyongyuEntry)list.get(i);
			JSONObject jsonObject = new JSONObject(systemChangyongyuEntry);
			array.put(jsonObject);
		}
		JSONObject object = new JSONObject();
		object.put("totalProperty", size);
		object.put("root",array);
		response.getWriter().print(object);
		return null;
	}
	public ActionForward deletePersonal(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		iChangYongYuBo.deletePerson(id);
		return null;
	}
	public ActionForward deleteDefalt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		String id = request.getParameter("id");
		iChangYongYuBo.deleteDedalt(id);
		return null;
	}
	public ActionForward updateDefalt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		String id = request.getParameter("id");
		String changyongyu = request.getParameter("changyongyu");
		iChangYongYuBo.updateDefalt(id, changyongyu);
		return null;
	}
	public ActionForward updatePerson(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		String id = request.getParameter("id");
		String changyongyu = request.getParameter("changyongyu");
		iChangYongYuBo.updatePerson(id, changyongyu);
		return null;
	}
	//管理员添加个人常用语
	public ActionForward addPerson(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		TSystemChangyongyuEntry systemChangyongyuEntry = new TSystemChangyongyuEntry();
		String mainId = request.getParameter("mainId");
		String changyongyu  =request.getParameter("changyongyu");
		String userName = request.getParameter("name");
		String userId = request.getParameter("userid");
		systemChangyongyuEntry.setMainId(Integer.parseInt(mainId));
		systemChangyongyuEntry.setSentence(changyongyu);
		systemChangyongyuEntry.setUserName(userName);
		systemChangyongyuEntry.setUserId(Integer.parseInt(userId));
		iChangYongYuBo.addPerson(systemChangyongyuEntry);
	    return null;
	}
	public ActionForward addDefalt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		TSystemChangyongyuDefault systemChangyongyuDefault = new TSystemChangyongyuDefault();
		String mainId = request.getParameter("mainId");
		String changyongyu = request.getParameter("changyongyu");
		systemChangyongyuDefault.setMainId(Integer.parseInt(mainId));
		systemChangyongyuDefault.setSentence(changyongyu);
		iChangYongYuBo.addDefalt(systemChangyongyuDefault);
	    return null;
	}
	//按照id获得个人常用语
	public ActionForward getGrChangYongyu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		if(start == null) start="0";
		if(limit == null) limit="10";
		List<TSystemChangyongyuEntry> list = iChangYongYuBo.queryByUser(user.getUser_id(), start, limit);
		JSONArray array = new JSONArray();
		for(int i=0;i<list.size();i++){
			TSystemChangyongyuEntry tChangyongyuEntry = list.get(i);
			Integer id = tChangyongyuEntry.getMainId();
			String description = iChangYongYuBo.queryById(id);
			JSONObject object = new JSONObject(tChangyongyuEntry);
			object.put("description", description); 
			array.put(object);
		}
		JSONObject obj = new JSONObject();
		obj.put("root", array);
		obj.put("totalProperty", list.size());	
		response.getWriter().print(obj);
		return null;
	}
	//获得常用语所有的环节
	public ActionForward getCYYhuanjie(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		List<TSystemChangyongyu> list = iChangYongYuBo.queryHuanJie();
		JSONArray array = new JSONArray();
		for(int i=0;i<list.size();i++){
			TSystemChangyongyu changyongyu = list.get(i);
			JSONObject object = new JSONObject(changyongyu);
			array.put(object);
		}
		JSONObject obj = new JSONObject();
		obj.put("huangjie", array);
		response.getWriter().print(obj);
		return null;
	}
	//个人常用语管理添加个人常用语
	public ActionForward addGRCYY(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String nodeId = request.getParameter("nodeId");
		String changyongyu = request.getParameter("changyongyu");
		TSystemChangyongyuEntry entry = new TSystemChangyongyuEntry();
		entry.setMainId(Integer.parseInt(nodeId));
		entry.setSentence(changyongyu);
		entry.setUserId(user.getUser_id());
		entry.setUserName(user.getUsername());
		iChangYongYuBo.addPerson(entry);
		return null;
	}
}
